package com.jvtd.eyl.bean.binding;

import android.databinding.Bindable;
import android.databinding.Observable;
import android.databinding.PropertyChangeRegistry;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.jvtd.eyl.BR;

import org.litepal.crud.DataSupport;

/**
 * 书籍信息实体
 *
 * 作者:chenlei
 * 时间:2018/11/14 9:36 AM
 */
public class BookBean extends DataSupport implements Parcelable, Observable
{
    private PropertyChangeRegistry mRegistry = new PropertyChangeRegistry();

    @SerializedName("id")
    private String bookId;//书籍ID
    @SerializedName("title")
    private String name;//书籍名称
    private String img;//书籍图片
    @SerializedName("fileurl")
    private String url;//书籍路径

    private String category_id;//期刊ID
    private String category_name;//期刊名称

    @SerializedName("is_addbook")
    private String isAdd;//是否加入
    private String author;//作者
    public static final String YES = "1";
    public static final String NO = "2";

    private String type;//书籍类型 1、pdf  2、txt
    public static final String TYPE_PDF = "1";
    public static final String TYPE_TXT = "2";
    public static final String TYPE_PPT = "3";
    public static final String TYPE_VIDEO = "4";
    public static final String TYPE_XLS = "100";
    public static final String TYPE_DOC = "101";


    private boolean isSelected = false;//是否已选择
    private String userId;//用户ID
    private double readNum = 0.0;//阅读位置

    @Bindable
    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
        mRegistry.notifyChange(this, BR.selected);
    }

    @Bindable
    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
        mRegistry.notifyChange(this, BR.author);
    }

    @Bindable
    public String getIsAdd() {
        return isAdd;
    }

    public void setIsAdd(String isAdd) {
        this.isAdd = isAdd;
        mRegistry.notifyChange(this, BR.isAdd);
    }

    public String getCategory_id() {
        return category_id;
    }

    public void setCategory_id(String category_id) {
        this.category_id = category_id;
    }

    public String getCategory_name() {
        return category_name;
    }

    public void setCategory_name(String category_name) {
        this.category_name = category_name;
    }

    public static String getTypePdf() {
        return TYPE_PDF;
    }

    public String getBookId()
    {
        return bookId;
    }

    public void setBookId(String bookId)
    {
        this.bookId = bookId;
    }

    @Bindable
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
        mRegistry.notifyChange(this, BR.name);
    }

    @Bindable
    public String getImg()
    {
        return img;
    }

    public void setImg(String img)
    {
        this.img = img;
        mRegistry.notifyChange(this, BR.img);
    }

    public String getUrl()
    {
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    public String getType()
    {
        return type;
    }

    public String getFileType(){
        if (TextUtils.equals(type,TYPE_PDF))
            return "pdf";
        else if (TextUtils.equals(type,TYPE_VIDEO))
            return "video";
        else if (TextUtils.equals(type,TYPE_TXT))
            return "txt";
        else if (TextUtils.equals(type,TYPE_PPT))
            return getFileTypeWithUrl();
        else if (TextUtils.equals(type,TYPE_XLS))
            return getFileTypeWithUrl();
        else if (TextUtils.equals(type,TYPE_DOC))
            return getFileTypeWithUrl();
        return "";
    }

    private String getFileTypeWithUrl(){
        if (TextUtils.isEmpty(url)) return "";
        int index = url.lastIndexOf(".");
        return url.substring(index+1,url.length());
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public String getUserId()
    {
        return userId;
    }

    public void setUserId(String userId)
    {
        this.userId = userId;
    }

    public double getReadNum()
    {
        return readNum;
    }

    public void setReadNum(double readNum)
    {
        this.readNum = readNum;
    }

    @Override
    public void addOnPropertyChangedCallback(OnPropertyChangedCallback onPropertyChangedCallback)
    {
        mRegistry.add(onPropertyChangedCallback);
    }

    @Override
    public void removeOnPropertyChangedCallback(OnPropertyChangedCallback onPropertyChangedCallback)
    {
        mRegistry.remove(onPropertyChangedCallback);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.bookId);
        dest.writeString(this.name);
        dest.writeString(this.img);
        dest.writeString(this.url);
        dest.writeString(this.category_id);
        dest.writeString(this.category_name);
        dest.writeString(this.isAdd);
        dest.writeString(this.author);
        dest.writeString(this.type);
        dest.writeByte(this.isSelected ? (byte) 1 : (byte) 0);
        dest.writeString(this.userId);
        dest.writeDouble(this.readNum);
    }

    public BookBean() {
    }

    protected BookBean(Parcel in) {
        this.bookId = in.readString();
        this.name = in.readString();
        this.img = in.readString();
        this.url = in.readString();
        this.category_id = in.readString();
        this.category_name = in.readString();
        this.isAdd = in.readString();
        this.author = in.readString();
        this.type = in.readString();
        this.isSelected = in.readByte() != 0;
        this.userId = in.readString();
        this.readNum = in.readDouble();
    }

    public static final Creator<BookBean> CREATOR = new Creator<BookBean>() {
        @Override
        public BookBean createFromParcel(Parcel source) {
            return new BookBean(source);
        }

        @Override
        public BookBean[] newArray(int size) {
            return new BookBean[size];
        }
    };

    @Override
    public String toString() {
        return "BookBean{bookId=" + bookId + '\'' +
                ", name='" + name + '\'' +
                ", img='" + img + '\'' +
                ", url='" + url + '\'' +
                ", category_id='" + category_id + '\'' +
                ", category_name='" + category_name + '\'' +
                ", isAdd='" + isAdd + '\'' +
                ", author='" + author + '\'' +
                ", type='" + type + '\'' +
                ", isSelected=" + isSelected +
                ", userId='" + userId + '\'' +
                ", readNum=" + readNum +
                '}';
    }
}
